package com.jay.android.view;

import com.jay.android.R;

import android.R.integer;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.MeasureSpec;

public class PercentView extends View {
	private Paint percentPaint;
	private Paint textPaint;

	private int percentRate;
	private static final int percentTotal = 100;

	private int allLineColor;
	private int percentLineColorLow;
	private int percentLineColorHigh;
	private int defGraWidth;
	private int curGraWidth;
	private int graHeight;
	private int textSize;
	private int textColor;
	private String noticeString = "点击扫描";

	public PercentView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		init(null, 0);
	}

	public PercentView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(attrs, 0);
	}

	public PercentView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init(attrs, defStyle);
	}

	private void init(AttributeSet attrs, int defStyle) {
		final TypedArray a = getContext().obtainStyledAttributes(attrs,
				R.styleable.ShowPercentView, defStyle, 0);

		percentRate = a.getInteger(R.styleable.ShowPercentView_percent, 10);
		allLineColor = a.getColor(R.styleable.ShowPercentView_allLineColor,
				Color.RED);
		percentLineColorLow = a.getColor(
				R.styleable.ShowPercentView_percentLineColorLow, Color.GREEN);
		percentLineColorHigh = a.getColor(
				R.styleable.ShowPercentView_percentLineColorHigh, Color.RED);
		defGraWidth = a.getInteger(R.styleable.ShowPercentView_defGraWidth, 1);
		curGraWidth = a.getInteger(R.styleable.ShowPercentView_curGraWidth, 4);
		graHeight = a.getInteger(R.styleable.ShowPercentView_graHeight, 8);
		textSize = a.getInteger(R.styleable.ShowPercentView_textSize, 150);
		textColor = Color.WHITE;// a.getInteger(R.styleable.ShowPercentView_graHeight,
		// Color.BLACK);
		a.recycle();

		percentPaint = new Paint();
		percentPaint.setAntiAlias(true);

		textPaint = new Paint();

		textPaint.setAntiAlias(true);
	}

	protected void onDraw(android.graphics.Canvas canvas) {
		super.onDraw(canvas);

		int width = getMeasuredWidth();
		int height = getMeasuredHeight();
		float degrees = (float) (300.0 / percentTotal);
		int centerX = width / 2;
		int centerY = height / 2;
		// draw text
		drawText(canvas, centerX, centerY);
		// draw all line
		drawAllGraduation(canvas, degrees, percentTotal, centerX, centerY);
		// draw current line
		drawCurrentGraduation(canvas, degrees, percentRate, centerX, centerY);
	}

	private void drawText(Canvas canvas, int x, int y) {
		String textString = percentRate + "";
		textPaint.setTextSize(100);
		float textWidth = textPaint.measureText(textString);
		float textX = x - textWidth / 2 - 20;
		float textY = y + textPaint.getTextSize() / 2 - 40;
		textPaint.setColor(textColor);

		canvas.drawText(textString, textX, textY, textPaint);

		textX = x + textWidth / 2 + 10;
		textY = y - textPaint.getTextSize() / 2 + 30;
		textPaint.setTextSize(35);
		canvas.drawText("分", textX, textY, textPaint);

		textPaint.setTextSize(35);
		textWidth = textPaint.measureText(noticeString);
		textX = x - textWidth / 2;
		textY = y + textPaint.getTextSize() / 2 + 50;
		textPaint.setAlpha(150);
		canvas.drawText(noticeString, textX, textY, textPaint);
	}

	private void drawAllGraduation(Canvas canvas, float degrees, int scaleVale,
			int x, int y) {
		percentPaint.setColor(allLineColor);
		percentPaint.setStrokeWidth(defGraWidth);
		percentPaint.setAlpha(80);
		drawGraduation(canvas, degrees, scaleVale, x, y);
	}

	private void drawCurrentGraduation(Canvas canvas, float degrees,
			int scaleValue, int x, int y) {
		percentPaint.setColor(percentLineColorHigh);
		percentPaint.setStrokeWidth(curGraWidth);
		drawGraduation(canvas, degrees, scaleValue, x, y);
	}

	private void drawGraduation(Canvas canvas, float degrees, int scaleValue,
			int x, int y) {
		canvas.save();
		canvas.translate(0, y);
		canvas.rotate(-60, x, 0);
		for (int i = 0; i < scaleValue; i++) {
			canvas.drawLine(0, 0, graHeight, 0, percentPaint);
			canvas.rotate(degrees, x, 0);
		}

		canvas.restore();
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		// TODO Auto-generated method stub
		// super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		int width = MeasureSpec.getSize(widthMeasureSpec);
		int height = MeasureSpec.getSize(heightMeasureSpec);
		int d = (width >= height) ? height : width;
		setMeasuredDimension(d, d);
	}

	public void setPercent(int percent) {
		this.percentRate = percent;
		postInvalidate();
	}

	public int getPercent() {
		return this.percentRate;
	}

	public void scanDone() {
		noticeString = "点击返回";
	}

	public void scaning() {
		noticeString = "正在扫描";
	}
}
